<?php
	require_once($_SERVER['DOCUMENT_ROOT']."/classes/GaleryItem.interface.php");
	require_once($_SERVER['DOCUMENT_ROOT']."/classes/HumeurDatabase.class.php");
		
	class Color implements GaleryItem{
		
		var $redQuantity;
		var $greenQuantity;
		var $blueQuantity;
		var $colorName;
		var $isActivated;
		
		public function Color($data=0){
			if(is_array($data)) $this->create($data);
			elseif(is_string($data)) $this->load($data);
		}
		
		public function load($id){
			$db = new HumeurDatabase();
			$data = mysql_fetch_assoc($db->query("SELECT * FROM colors WHERE colorName = '".$id."' LIMIT 1;"));
			$this->create($data);
		}
		
		public function create($array){
			if(isset($array['redQuantity'])) $this->redQuantity = $array['redQuantity'];
			if(isset($array['greenQuantity'])) $this->greenQuantity = $array['greenQuantity'];
			if(isset($array['blueQuantity'])) $this->blueQuantity = $array['blueQuantity'];
			if(isset($array['colorName'])) $this->colorName = $array['colorName'];
			if(isset($array['isActivated'])) $this->isActivated = $array['isActivated'];
		}
		
		public function save(){
			$db = new HumeurDatabase();
			if(mysql_num_rows($db->query("SELECT * FROM colors WHERE colorName = '".$this->colorName."' LIMIT 1;")) > 0) return $this->update();
			else return $this->insert();
		}
		
		public function insert(){
			$db = new HumeurDatabase();
			return $db->query("INSERT INTO colors VALUES ('".$this->colorName."','".$this->redQuantity."','".$this->greenQuantity."','".$this->blueQuantity."','1');");
		}
		
		public function delete(){
			$db = new HumeurDatabase();
			return $db->query("DELETE FROM colors WHERE colorName = '".$this->colorName."';");
		}
		
		public function update(){
			$db = new HumeurDatabase();
			return $db->query("UPDATE colors SET redQuantity = '".$this->redQuantity."', greenQuantity = '".$this->greenQuantity."',blueQuantity = '".$this->blueQuantity."',isActivated = '".$this->isActivated."' WHERE colorName = '".$this->colorName."';");
		}
		
		public function toggleActivation(){
			$this->isActivated = ($this->isActivated == 1)? 0:1;
		}
		
		public function summary(){
			return '<div id="div'.$this->colorName.'" class="'.(($this->isActivated == '1')? "activated":"disactivated").' existingColorBloc centered">
				'.$this->colorName.'<br/>
				<div id="'.$this->colorName.'"></div>
				<div>
					<span id="'.$this->colorName.'redQuantity" class="colorQuantity red">'.$this->redQuantity.'</span>
					<span id="'.$this->colorName.'greenQuantity" class="colorQuantity green">'.$this->greenQuantity.'</span>
					<span id="'.$this->colorName.'blueQuantity" class="colorQuantity blue">'.$this->blueQuantity.'</span>
				</div>
				<script>createShape(\''.$this->colorName.'\',\'createCircle\','.$this->redQuantity.','.$this->greenQuantity.','.$this->blueQuantity.')</script>
				<a onclick="ajaxLoad(\'makeColorEditForm\',\'id='.$this->colorName.'\',makeColorEdit_response)"><img src="img/editMember.png" alt="Editer"/></a>
				<a onclick="ajaxLoad(\'deleteColor\',\'id='.$this->colorName.'\',deleteColor_response)">'.(($this->isActivated == 0)? '<img src="img/activeColor.png" alt="Activer"/>':'<img src="img/deleteMember.png" alt="Désactiver"/>').'</a>
			</div>';
		}
		
		public function editForm(){
			return '<div id="div'.$this->colorName.'" class="existingColorBloc centered edited">
				'.$this->colorName.'<br/>
				<div id="'.$this->colorName.'"></div>
				<div>
					<input type="text" id="'.$this->colorName.'redQuantity" class="colorQuantity red" value="'.$this->redQuantity.'" onkeyup="createShape(\''.$this->colorName.'\',\'createCircle\',document.getElementById(\''.$this->colorName.'redQuantity\').value,document.getElementById(\''.$this->colorName.'greenQuantity\').value,document.getElementById(\''.$this->colorName.'blueQuantity\').value)"/>
					<input type="text" id="'.$this->colorName.'greenQuantity" class="colorQuantity green" value="'.$this->greenQuantity.'" onkeyup="createShape(\''.$this->colorName.'\',\'createCircle\',document.getElementById(\''.$this->colorName.'redQuantity\').value,document.getElementById(\''.$this->colorName.'greenQuantity\').value,document.getElementById(\''.$this->colorName.'blueQuantity\').value)"/>
					<input type="text" id="'.$this->colorName.'blueQuantity" class="colorQuantity blue" value="'.$this->blueQuantity.'" onkeyup="createShape(\''.$this->colorName.'\',\'createCircle\',document.getElementById(\''.$this->colorName.'redQuantity\').value,document.getElementById(\''.$this->colorName.'greenQuantity\').value,document.getElementById(\''.$this->colorName.'blueQuantity\').value)"/>
				</div>
				<input type="button" value="Modifier" onclick="ajaxLoad(\'editColor\',\'colorName='.$this->colorName.'&redQuantity=\'+document.getElementById(\''.$this->colorName.'redQuantity\').value+\'&greenQuantity=\'+document.getElementById(\''.$this->colorName.'greenQuantity\').value+\'&blueQuantity=\'+document.getElementById(\''.$this->colorName.'blueQuantity\').value+\'&isActivated='.$this->isActivated.'\',editColor_response)" />
			</div>';
		}
		
		public function __toString(){
			return $this->colorName;
		}
		
	}
?>